<?php
/**
 * Created by PhpStorm.
 * User: Windows
 * Date: 2015/12/24
 * Time: 9:57
 */

class ErppriceController extends Apicontroller{
    //初始化
    public function init(){
        parent::init();
    }

    //这边获取其他地方换过来的数据,此为报价
    public function getQuoteAction(){
        global $_suppliyStateCfg;
        $api = new ApiModel();
        ///api/erpPrice/getQuote = BASE_URL
        $url = _HTTP_CLOUD_HOST."/api/getprice/getprice";
        $requestdata = $api->url_request($url);
        //var_dump(json_decode($requestdata,true));
        //die();
        $data = json_decode($requestdata,true);
        if(!empty($data) &&$data['status']=='success'){
            //这边插入数据
            if(!empty($data['data']) &&is_array($data['data'])){
                foreach($data['data'] as $key=>$var){
                    //产品===
                    if(!empty($var['product_name'])&&!empty($var['brand_name'])&&!empty($var['cate_name'])){
                        $product = new ProductModel();
                        //这边创建产品
                        $product_id = $product->getProdutByName($var['product_name'],$var['brand_name'],$var['cate_name']);
                        if(!$product_id){
                            //这边插入产品,
                            $product_id = $product->addProdutByName($var['product_name'],$var['brand_name'],$var['cate_name']);
                        }
                    }

                    //数据获取，并插入
                    $data = array(
                        'user_id'=>$var['create_uid'],//自营的
                        'business_id'=>'0',//自营的
                        'organization_id'=>'1',
                        'department_id'=>'1',
                        'create_time'=>$var['create_time'],
                        'create_uid'=>$var['create_uid'],
                        'update_time'=>$var['create_time'],
                        'update_uid'=>$var['update_uid'],
                        'type'=>'stock',
                        'status'=>$_suppliyStateCfg['up'],
                        'release_type'=>'E',//这是表示从erp获取数据的类型
                        'product_id'=>$product_id,
                        'service_id'=>$var['service_id'],
                        'delivery_time'=>$var['delivery_time'],
                        'delivery_area'=>$var['delivery_area'],
                        'delivery_method'=>empty($var['delivery_method'])?'1':$var['delivery_method'],
                        'currency_id'=>$var['currency_id'],
                        'num'=>$var['remain'],
                        'price'=>$var['price'],
                        'area_id'=>$var['area_id'],
                        'vice_uom_id'=>$var['vice_uom_id'],
                        'vice_rate'=>$var['vice_rate'],
                        'deposit_rate'=>$var['deposit_rate'],
                        'tray_type'=>$var['tray_type'],
                        'buy_qty'=>"0",
                        'remark'=>'erp发布的报价',
                        'iq_contacts'=>$var['realname'],
                        'iq_mobile'=>$var['mobile'],
                        'iq_qq'=>$var['qq'],
                    );
                    //这边根据这个人的报价，判断重复，并覆盖
                    $quote = new QuotationModel();
                    $quotelist = $quote->getDetailsById("iq_quotation.user_id = '{$var['create_uid']}' and iq_quotation.type = 'stock' and iq_quotation.product_id = '{$product_id}' and iq_quotation.service_id = '{$var['service_id']}' and iq_quotation.currency_id = '{$var['currency_id']}' and iq_quotation.delivery_area = '{$var['delivery_area']}' and iq_quotation.delivery_time = '{$var['delivery_time']}' and iq_quotation.area_id = '{$var['area_id']}' and iq_quotation.status <> '4'");
//                    var_dump($quotelist);die();
                    if(!empty($quotelist)){
                        //这边就是只更新数据
                        $quote_data = array(
                            'update_time'=>$var['create_time'],
                            'update_uid'=>$var['create_uid'],
//                            'contacts'=>$var['realname'],
//                            'mobile'=>$var['mobile'],
//                            'qq'=>$var['qq'],
                            'price'=>$var['price'],
                            'num'=>$var['num'],
                            'remark'=>'erp报价,重复数据进行更新',
                        );
                        //这边只是更新
                        $res = $api -> saveeditquote($quote_data,$quotelist['id']);
                    }else{
                        $res = $api -> savequote($data);
                    }
                    //这边对于相同的数据的更新返回值 0，所以就不去判断是否成功，因为这个的updatetime不是当前的可变的时间，
//                    if($res){
//                        echo "success"."<br/>";
//                    }else{
//                        echo "failed"."<br/>";
//                    }
                }
            }
        }else{
            echo "还没有满足的报价条件哦!";
        }
        die();
    }

    //获取订单
    public function getOrderListAction(){
        $api = new ApiModel();
        $where = "1=1";
        //
        $orderList = $api->getOrderListByWhere($where);
        if(!empty($orderList)){
            exit(json_encode(array(
                    'status' => 'success',
                    'size' => sizeof($orderList),
                    'data' => $orderList,
                    'msg' => "",
                )
            ));
        }else{
            exit(json_encode(array(
                    'status' => 'fail',
                    'size' => 0,
                    'data' => "",
                    'msg' => "",
                )
            ));
        }

        die();

    }



    public function testAction(){
        echo "erpPrice-----test";
        die();
    }

}